package com.jhd.fuzhou.view.punishment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.adapter.ShowAdapter;
import com.jhd.fuzhou.service.PunishmentService;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.AdministrativeAccessoryVo;
import com.jhd.fuzhou.vo.JsonForResult;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class PunishmentDocumentActivity extends BaseActivity {

    @BindView(R.id.recyclerView)RecyclerView recyclerView;
    private Context context=this;
    private List<AdministrativeAccessoryVo> administrativeAccessoryVoList;
    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_punishment_document);
    }

    @Override
    protected void processLogic() {
        onBack();
        String id=getIntent().getStringExtra("id");
        getListByPunishmentId(id);
    }

    public void getListByPunishmentId(String id){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        PunishmentService vs = retrofit.create(PunishmentService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("id",id);
        Call<JsonForResult> jsonForResultCall = vs.getListByPunishmentId(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            administrativeAccessoryVoList = JSON.parseArray(jsonForResult.getData().toString(), AdministrativeAccessoryVo.class);
                            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
                            recyclerView.setLayoutManager(linearLayoutManager);
                            ShowAdapter adapter=new ShowAdapter(administrativeAccessoryVoList,context);
                            recyclerView.setAdapter(adapter);
                        }else{
                            administrativeAccessoryVoList=new ArrayList<>();
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
//                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg;
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

}